#include "GlobalError.h"

GlobalError * GlobalError::instance = 0;

/*---------------------------------------------------------------------------------------
--	FUNCTION:	GlobalError::GlobalError ()
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Riberiro
--
--      INTERFACE:      GlobalError::GlobalError ()
--
--	RETURNS:
--
--	NOTES:
--		Constructor for the GlobalError
---------------------------------------------------------------------------------------*/
GlobalError::GlobalError ()
{

}

void GlobalError::initInstance ()
{
    if (instance == 0)
        instance = new GlobalError();
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: critical
--
--	Date: April 7, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void GlobalError::critical (const char errorMsg[], int exitCode)
--
--      const char errorMsg[]: The error message
--      int exitCode: Error message identifier.
--
--  RETURNS: void
--
--	NOTES: Displays the error message and terminates the program.
--
--------------------------------------------------------------------------------------------*/
void GlobalError::critical (const char errorMsg[], int exitCode)
{
    QString title = "Critical Error";
    QString msg = QString::fromAscii(errorMsg);

    if (QThread::currentThread() != QApplication::activeWindow()->thread())
    {
        QMetaObject::invokeMethod(instance, "displayError", Qt::QueuedConnection, Q_ARG(QString, title), Q_ARG(QString, msg), Q_ARG(bool, true));
        QApplication::activeWindow()->thread()->wait();
    }
    else
    {
        instance->displayError(title, msg, true);
    }

    exit(exitCode);
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	GlobalError::warning (const char errorMsg[])
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Riberiro
--
--      INTERFACE:      GlobalError::GlobalError ()
--
--	RETURNS:
--
--	NOTES:
--		Function to passin specific error message and create the object for it
---------------------------------------------------------------------------------------*/
void GlobalError::warning (const char errorMsg[])
{
    QMetaObject::invokeMethod(instance, "displayError",
                              Qt::QueuedConnection,
                              Q_ARG(QString, "Warning"),
                              Q_ARG(QString, QString::fromAscii(errorMsg)),
                              Q_ARG(bool, true));
}

/*-----------------------------------------------------------------------------------------------------
--	FUNCTION:	GlobalError::displayError (QString errorTitle, QString errorMsg, bool critical)
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Riberiro
--
--      INTERFACE:      GlobalError::GlobalError ()
--
--	RETURNS:
--
--	NOTES:
--		Function to display the actual error messages
---------------------------------------------------------------------------------------*/
void GlobalError::displayError (QString errorTitle, QString errorMsg, bool critical)
{
    QMessageBox::Icon icon = (critical) ? QMessageBox::Critical : QMessageBox::Warning;
    QMessageBox * messageBox = new QMessageBox(icon, errorTitle, errorMsg);

    messageBox->exec();

    if (critical)
        thread()->exit();
}
